package cn.edu.njpi.web;

import cn.edu.njpi.model.TCollageAdmin;
import cn.edu.njpi.model.TSchoolAdmin;
import cn.edu.njpi.model.TUser;
import cn.edu.njpi.service.TCollageAdminService;
import cn.edu.njpi.service.TSchoolAdminService;
import cn.edu.njpi.service.TUserService;
import cn.edu.njpi.utils.InformUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.web
 * @date 2022/4/19 6:36 PM
 */
@Controller
@RequestMapping(value = "/user")
public class UserWeb {
    @Autowired
    private TUserService tUserService;
    @Autowired
    private TSchoolAdminService tSchoolAdminService;
    @Autowired
    private TCollageAdminService tCollageAdminService;

    @Value("${caoxingfen.url}")
    private String url;

    @RequestMapping(value = "/login")
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView("/user/login");
        modelAndView.addObject("title", InformUtils.title);
        modelAndView.addObject("url", url);
        return modelAndView;
    }

    @RequestMapping(value = "/admin")
    public ModelAndView userAdmin(String token) {
        ModelAndView modelAndView = new ModelAndView("/user/admin");
        modelAndView.addObject("title", InformUtils.title);
        modelAndView.addObject("url", url);
        TUser tUser = (TUser) InformUtils.map.get(token);
        if (tUser.getUserPermssionId() == 1) {
            List<TUser> allUser = tUserService.getAllUser();
            modelAndView.addObject("tUserList", allUser);
        } else if (tUser.getUserPermssionId() == 2) {
            TSchoolAdmin tSchoolAdmin = tSchoolAdminService.selectTSchoolAdminByUserId(tUser.getUserId());
            List<TUser> tUserList = tUserService.selectUserBySchoolId(tSchoolAdmin.getSchoolId());
            modelAndView.addObject("tUserList", tUserList);
        } else if (tUser.getUserPermssionId() == 3) {
            TCollageAdmin tCollageAdmin = tCollageAdminService.selectTCollageAdminByUserId(tUser.getUserId());
            List<TUser> tUserList = tUserService.selectUserByCollageId(tCollageAdmin.getCollageId());
            modelAndView.addObject("tUserList", tUserList);
        } else if (tUser.getUserPermssionId() == 4) {
            List<TUser> tUserList = tUserService.selectUserByClassId(tUser.getClassId());
            modelAndView.addObject("tUserList", tUserList);
        }
        modelAndView.addObject("tUser", tUser);
        return modelAndView;
    }
}
